import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Index from '@/views/Index'
import { getToken } from '@/utils/localStrone'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    name: 'index',
    component: Index,
    redirect: '/task',
    children: [
      {
        path: '/task',
        component: () => import('@/views/task.vue')
      },
      {
        path: '/message',
        component: () => import('@/views/Message.vue')
      },
      {
        path: '/home',
        component: () => import('@/views/Home.vue')

      }
    ]
  },
  {
    path: '/car',
    component: () => import('@/views/Car.vue')
  },
  {
    path: '/calendar',
    component: () => import('@/views/Calendar.vue')
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   // component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]

const router = new VueRouter({
  routes
})

// 路由前置守卫
const arr = ['/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    if (to.psth === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (arr.includes(to.path)) {
      next()
    } else {
      // next('/login')
      router.push('/login')
      // next()
    }
  }
})

export default router
